<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="180px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择部门负责人">
          <el-option v-for="obj in employees" :key="obj.id" :value="obj.username" :lable="obj.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" style="width:80%" :rows="3" placeholder="1-300个字符" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="cancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getEmployeeSimple,
  addDepartments,
  getDepartDetail,
  updateDepartments
} from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    parentId: {
      type: [String, Number],
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    // 精简后的所有部门数据
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 校验部门编码是否重复
    const validCode = (rule, value, callback) => {
      let existCodeList = this.originList.map(item => item.code)
      // 如果是编辑状态，则将需要编辑的这一项排除在外
      if (this.isEdit) {
        existCodeList = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }
      existCodeList.includes(value) ? callback(new Error(`部门编码${value}已存在`)) : callback()
    }

    // 同级部门禁止出现重复部门
    const validName = (rule, value, callback) => {
      // 查找同级的分类
      let existNameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)

      // 判断如果是编辑 先找兄弟节点 然后对比
      if (this.isEdit) {
        // 先找到自身对应的对象
        const obj = this.originList.find(item => item.id === this.parentId)
        // 获取到父级的 id
        const pid = obj.pid
        // 有了父级 id,获取同级, 同时又要排除本身
        existNameList = this.originList
          .filter(item => item.pid === pid && item.id !== this.parentId)
          .map(item => item.name)
      }
      // 判断最新输入的部门名称是否存在
      existNameList.includes(value) ? callback(new Error(`部门名称${value}已存在`)) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [], // 负责人列表
      // 验证规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmployeeSimple()
    // 当前部门数据
    this.getDepartDetail(this.parentId)
  },
  methods: {

    async addDepts() {
      const res = await addDepartments({ ...this.form, pid: this.parentId })
      // console.log(res)
      this.$message.success(res.message)
      this.$emit('update-list')
    },
    async editDepts() {
      const res = await updateDepartments(this.form)
      // console.log(res)
      this.$message.success(res.message)
      this.$emit('update-list')
    },
    // 获取组织数据回填
    async getDepartDetail(id) {
      if (this.isEdit) {
        const res = await getDepartDetail(id)
        this.form = res.data
      }
    },
    // 获取部门负责人数据
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      // console.log(res)
      this.employees = res.data
    },
    // 点击确定
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.editDepts() : this.addDepts()
        }
      })
    },
    // 点击取消
    cancle() {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
